package AS_source
{
	import com.adobe.utils.XMLUtil;
	import com.adobe.xml.syndication.rss.Item20;
	import com.adobe.xml.syndication.rss.RSS20;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	public class NewsUpdates

	{
		import Components.*;
		import mx.containers.Accordion;
		import mx.containers.ApplicationControlBar;
		import mx.containers.TabNavigator;
		import mx.containers.VBox;
		import mx.containers.HBox;
		import mx.containers.Panel;
		import mx.controls.Alert;
		import mx.controls.ComboBox;
		import mx.controls.Text;
		import mx.controls.TextArea;
		import mx.rpc.events.FaultEvent;
		import flash.events.Event;
		var loader:URLLoader;
		
		var tabNavi:TabNavigator;
		var searchNavi:Accordion;
		var SelectionDataOutput:String;
		var RSS_URL:String;
		var NewsTabOutput:TextArea;
		public function NewsUpdates(tabControl:TabNavigator, searchControl:Accordion)
		{
			tabNavi=tabControl;
			searchNavi=searchControl;
			
			
		}
		
		public function updatesearchButtonEvt(e:Event):void
		{
			var profSearch:Profile_Search_VBox=Profile_Search_VBox(searchNavi.getChildByName("profSearch"));
			var controlBar:ApplicationControlBar=ApplicationControlBar(profSearch.getChildByName("profControlBar"));
			var genBox:VBox=VBox(controlBar.getChildByName("profVBox"));
			var selectionData:String=ComboBox(genBox.getChildByName("profComboBox")).selectedItem.data;
			var myPattern:RegExp = /\_/g; 
			selectionData=selectionData.replace(myPattern, "+");
			SelectionDataOutput=selectionData;
			
			//url of rss 2.0 feed
			RSS_URL = "http://pipes.yahoo.com/pipes/pipe.run?_id=39ad5f604a4431b99c56703fb405cee4&_render=rss&newsupdatesearch="+SelectionDataOutput;
            //Alert.show("URL"+ RSS_URL);
			loader = new URLLoader();
			clearOutput("Please Wait Loading....Please be patient :-P");
			//request pointing to feed
			var request:URLRequest = new URLRequest(RSS_URL);
			//Alert.show("hello1"+ request);
			request.method = URLRequestMethod.GET;
		
			//listen for when the data loads
			loader.addEventListener(Event.COMPLETE, onDataLoad);
		
			//listen for error events
			loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
		
			//load the feed data
			loader.load(request);
		}
		
		
		
		//called when user presses the button to search for a school
		//based on the school selected,news updates to be displayed in the news updates tab
		
//called once the data has loaded from the feed
		private function onDataLoad(e:Event):void
		{
			//get the raw string data from the feed
			var rawRSS:String = URLLoader(e.target).data;
		
			//parse it as RSS
			parseRSS(rawRSS);
		
		}

//parses RSS 2.0 feed and prints out the feed titles into
//the text area
		private function parseRSS(data:String):void
		{
		
			//XMLSyndicationLibrary does not validate that the data contains valid
			//XML, so you need to validate that the data is valid XML.
			//We use the XMLUtil.isValidXML API from the corelib library.
			if(!XMLUtil.isValidXML(data))
			{
				writeOutput("Feed does not contain valid XML.");
				//Alert.show("hello22");
				return;
			}	
		
			//create RSS20 instance
			var rss:RSS20 = new RSS20();
		    //Alert.show("rss:"+rss);
				//parse the raw rss data
				rss.parse(data);
		
				//get all of the items within the feed
				var items:Array = rss.items;
				//loop through each item in the feed
				clearOutput("");
				for each(var item:Item20 in items)
				{
					//print out the title of each item
					//writeOutput(item.title);
					writeOutput("<b> <a href=\""+item.link+"\" target=\"_blank\">" +item.title + "</a>");

					
				}
		}
	
		private function clearOutput(data:String):void
		{
			//Alert.show("hello3");
			var tester:VBox=VBox(tabNavi.getChildByName("schoolContents"));
			var outBox:HBox=HBox(tester.getChildByName("outerBox"));
			var newsPanel:Panel=Panel(outBox.getChildByName("articlePanel"));
			var output:TextArea=TextArea(newsPanel.getChildByName("articleOutput"));
			NewsTabOutput=output;
			//output += data + "\n";
			NewsTabOutput.htmlText=data;
		}
		private function writeOutput(data:String):void
		{
			//Alert.show("hello3");
			var tester:VBox=VBox(tabNavi.getChildByName("schoolContents"));
			var outBox:HBox=HBox(tester.getChildByName("outerBox"));
			var newsPanel:Panel=Panel(outBox.getChildByName("articlePanel"));
			var output:TextArea=TextArea(newsPanel.getChildByName("articleOutput"));
			NewsTabOutput=output;
			//output += data + "\n";
			NewsTabOutput.htmlText+=data+"<br>";
		}
		
		
		private function onIOError(e:IOErrorEvent):void
		{
			writeOutput("IOError : " + e.text);
		}
		
		private function onSecurityError(e:SecurityErrorEvent):void
		{
			writeOutput("SecurityError : " + e.text);
		}


	}
}